import { stringify } from 'qs';
import request from '@/utils/request';
import cookie from 'react-cookies';
import {routerRedux} from "dva/router";
import {notification} from "antd";

const codeMessage = {
  200: '服务器成功返回请求的数据。',
  201: '新建或修改数据成功。',
  202: '一个请求已经进入后台排队（异步任务）。',
  204: '删除数据成功。',
  400: '发出的请求有错误，服务器没有进行新建或修改数据的操作。',
  401: '用户没有权限（令牌、用户名、密码错误）。',
  403: '用户得到授权，但是访问是被禁止的。',
  404: '发出的请求针对的是不存在的记录，服务器没有进行操作。',
  406: '请求的格式不可得。',
  410: '请求的资源被永久删除，且不会再得到的。',
  422: '当创建一个对象时，发生一个验证错误。',
  500: '服务器发生错误，请检查服务器。',
  502: '网关错误。',
  503: '服务不可用，服务器暂时过载或维护。',
  504: '网关超时。',
};

function setHeaders(url,param){
  console.log("cookie.load(\"access_token\")",cookie.load("access_token"));
  console.log(url,param);
  if(param){
    if(param.headers){
      param.headers.authorization = "bearer " + (cookie.load("access_token")?cookie.load("access_token"):'')
    }else{
      param.headers = {
        authorization:"bearer " + (cookie.load("access_token")?cookie.load("access_token"):'')
      }
    }
  }else{
    param = {
      headers:{
        authorization:"bearer " + (cookie.load("access_token")?cookie.load("access_token"):'')
      },
    }
  }
  return request(url,param);
}

export async function queryProjectNotice() {
  return setHeaders('/api/project/notice');
}

export async function queryActivities() {
  return setHeaders('/api/activities');
}

export async function queryRule(params) {
  return setHeaders(`/api/rule?${stringify(params)}`);
}

export async function removeRule(params) {
  return setHeaders('/api/rule', {
    method: 'POST',
    data: {
      ...params,
      method: 'delete',
    },
  });
}

export async function addRule(params) {
  return setHeaders('/api/rule', {
    method: 'POST',
    data: {
      ...params,
      method: 'post',
    },
  });
}

export async function updateRule(params = {}) {
  return setHeaders(`/api/rule?${stringify(params.query)}`, {
    method: 'POST',
    data: {
      ...params.body,
      method: 'update',
    },
  });
}

export async function fakeSubmitForm(params) {
  return setHeaders('/api/forms', {
    method: 'POST',
    data: params,
  });
}

export async function fakeChartData() {
  return setHeaders('/api/fake_chart_data');
}

export async function queryTags() {
  return setHeaders('/api/tags');
}

export async function queryBasicProfile(id) {
  return setHeaders(`/api/profile/basic?id=${id}`);
}

export async function queryAdvancedProfile() {
  return setHeaders('/api/profile/advanced');
}

export async function queryFakeList(params) {
  return setHeaders(`/api/fake_list?${stringify(params)}`);
}

export async function removeFakeList(params) {
  const { count = 5, ...restParams } = params;
  return setHeaders(`/api/fake_list?count=${count}`, {
    method: 'POST',
    data: {
      ...restParams,
      method: 'delete',
    },
  });
}

export async function addFakeList(params) {
  const { count = 5, ...restParams } = params;
  return setHeaders(`/api/fake_list?count=${count}`, {
    method: 'POST',
    data: {
      ...restParams,
      method: 'post',
    },
  });
}

export async function updateFakeList(params) {
  const { count = 5, ...restParams } = params;
  return setHeaders(`/api/fake_list?count=${count}`, {
    method: 'POST',
    data: {
      ...restParams,
      method: 'update',
    },
  });
}

export async function accountLogin(params) {
  // return request(`/api8084/user/login?account=${params.userName}&password=${params.password}`);
  return request(`/api82/user/user/login?account=${params.userName}&password=${params.password}`);
}

export async function fakeRegister(params) {
  return setHeaders('/api/register', {
    method: 'POST',
    data: params,
  });
}

export async function queryNotices(params = {}) {
  return setHeaders(`/api/notices?${stringify(params)}`);
}

export async function getFakeCaptcha(mobile) {
  return setHeaders(`/api/captcha?mobile=${mobile}`);
}

export async function getGoodsList(params) {
  // return request(`/api8083/commodity/list?name=${params.name}`);
  // return setHeaders(`/api82/commodity/commodity/list?name=${params.name}`);
  return setHeaders(`/api82/commodity/commodity/list?name=${params.name}`);
}

export async function orderSave(params) {
  // return request(`/api8085/orderHead/save`, {
  return setHeaders(`/api82/order/orderHead/save`, {
    method: 'POST',
    data: params,
  });
}

export async function fetchList(params) {
  // return request(`/api8085/orderHead/list?${stringify(params)}`);
  return setHeaders(`/api82/order/orderHead/list?${stringify(params)}`);
}

// 获取订单详情
export async function fetchDetail(params) {
  // return request(`/api8085/orderHead/getByOrderHeadId?${stringify(params)}`);
  return setHeaders(`/api82/order/orderHead/getByOrderHeadId?${stringify(params)}`);
}

// 确认发货
export async function handleDeliverGoods(params) {
  // return request(`/api8085/orderHead/${params.id}`, {
  return setHeaders(`/api82/order/orderHead/${params.id}`, {
    method: 'patch',
    data: {
      ...params
    },
    headers: {
      'Content-Type': 'application/json',
    },
  });
}

// 编辑卡详情
export async function getCardInfo(params) {
  return setHeaders('/venuebooking/cardBasic/addCard', {
    method: 'POST',
    body: params,
  });
}

// 获取列表
export async function fetchQueryTable(params) {
  // return request(`/api8085/orderHead/getByOrderHeadId?${stringify(params)}`);
  return setHeaders(`/api82/order/orderHead/getByOrderHeadId?${stringify(params)}`);
}

// 删除
export async function handleDelate(params) {
  // return request(`/api8085/orderHead/getByOrderHeadId?${stringify(params)}`);
  return setHeaders(`/api82/order/orderHead/getByOrderHeadId?${stringify(params)}`);
}

// 获取登陆token
export async function getToken(params) {//http://192.168.0.106:8282/aouth/token
  return setHeaders(`/apitoken8282/oauth/token`, {
    // return request(`http://192.168.0.106:8282/oauth/token`, {
    method: 'POST',
    data: {
      ...params,
    },
    headers: {
      'Content-Type': "application/json;charset=UTF-8"
    }
  });
}

// 测试token
export async function userTest() {//http://192.168.0.106:8282/aouth/token
  return setHeaders(`/apiusertest/user/test`, {
    // return request(`http://192.168.0.106:8282/oauth/token`, {
    method: 'POST',
  });
}
